<template>
  <el-dialog
    :title="!dataForm.imgId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="轮播图片" prop="title">
        <el-upload
          list-type="picture-card"
          action="https://jsonplaceholder.typicode.com/posts/"
          :auto-upload="true"
          :show-file-list="false"
          :on-change="handleAvatarSuccess"
        >
          <img v-if="dataForm.imageUrl" :src="dataForm.imageUrl" alt="" />
          <i v-else class="el-icon-plus"></i>
        </el-upload>
        <!-- <img v-if="!imgFlag"
          width="100%"
          :src="
            'http://admin.yami.byesame.com/apis/mall4j/img/' + dataForm.imgUrl
          "
          alt=""
        />
        <img v-if="imgFlag"
          width="100%"
          :src="dataForm.imgUrl"
          alt=""
        /> -->
      </el-form-item>
      <el-form-item label="顺序" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="类型" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { Debounce } from "@/utils/debounce";
export default {
  data() {
    return {
      visible: false,
      dialogVisible: true,
      imgFlag: true,
      fileList: [],
      // dialogImageUrl: '',
      dataForm: {
        des: "",
        imgId: "",
        imgUrl: "",
        link: null,
        pic: "",
        prodName: "",
        relation: null,
        seq: "",
        shopId: 1,
        status: "",
        title: null,
        type: -1,
        uploadTime: "",
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      dataRule: {
        content: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    //图片
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res);
    },
    init(id) {
      this.dataForm.imgId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields();
      });
      this.imgFlag = true;
      this.dataForm.recDate = this.time();
      if (this.dataForm.imgId) {
        this.imgFlag = false;
        this.$http({
          url: this.$http.adornUrl(
            `/admin/indexImg/info/${this.dataForm.imgId}`
          ),
          method: "get",
          params: this.$http.adornParams(),
        }).then(({ data }) => {
          this.dataForm = data;
          console.log(data);
        });
      }
    },
    // 表单提交
    dataFormSubmit: Debounce(function () {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/admin/hotSearch`),
            method: this.dataForm.imgId ? "put" : "post",
            data: this.$http.adornData(this.dataForm),
            // {
            //   userId: this.dataForm.userId || undefined,
            //   nickName: this.dataForm.nickName,
            //   status: this.dataForm.status,
            // }
          }).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.visible = false;
                this.$emit("refreshDataList", this.page);
              },
            });
          });
        }
      });
    }),
    time() {
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      if (month.length < 1) {
        month = `0${month}`;
      }
      let day = date.getDate();
      let hours = date.getHours();
      let minu = date.getMinutes();
      let seco = date.getSeconds();
      return `${year}-${month}-${day} ${hours}:${minu}:${seco}`;
    },
  },
};
</script>

<style scoped>
img {
  width: 100px;
  height: 100px;
}
</style>
